package main.flex.component.main
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.flash.UIMovieClip;

	public class NavigationHelp extends UIMovieClip
	{
		public static const STATE_INIT_BASE 				: String = "chat_base";
		
		public static const STATE_IDLE_HEAD_SHAKE 			: String = "hochement";
		public static const STATE_IDLE_TAIL_SHAKE			: String = "queue";
		public static const STATE_IDLE_PAW_SHAKE			: String = "patte";
		
		public static const STATE_ACTIVE_MEOW 				: String = "miaou";
		
		public static const STATE_INACTIVE_SLEEPING			: String = "dodo";
		public static const STATE_INACTIVE_WAKING_UP		: String = "reveil";
		public static const STATE_INACTIVE_FALLING_ASLEEP	: String = "endort";
		public static const STATE_INACTIVE_DROWSING			: String = "A SETTER";
		
		private var targetState	: String = "";
		protected var lastState : String = STATE_INIT_BASE;
		protected var canBrowse : Boolean = true;
		
		[Bindable("chat_base")]
		[Bindable("hochement")]
		[Bindable("queue")]
		[Bindable("patte")]
		[Bindable("miaou")]
		[Bindable("dodo")]
		[Bindable("reveil")]
		[Bindable("endort")]
		[Bindable("A SETTER")]
		public function get currentTarget() : String
		{
			return targetState;
		}
		
		protected function handleStateChange( e : Event ) : void
		{
			
			if( this['bigIcon'] != null && this['bigIcon'].currentFrame == this['bigIcon'].totalFrames )
			{
				this['bigIcon'].stop();
			}
			
			if( currentLabel == null || currentLabel == lastState )
				return;
			
			if( currentLabel.substring( 0, 11 ) == "tr" )
			{
				trace("Transition");
				play();
			}
			else if ( currentLabel.substring( 0, 3 ) == "end" )
			{
				gotoAndStop( targetState );
				canBrowse = true;
			}
			lastState = currentLabel;
		}
		
		override public function initialize() : void
		{
			super.initialize();
			stop();
			gotoAndStop( STATE_INIT_BASE );
			
			addEventListener( Event.ENTER_FRAME, handleStateChange );
			
			animatedCat.addEventListener( MouseEvent.CLICK, function():void{ goKeyFrame( true ) } );
		}
		
		private function goKeyFrame( dispatch : Boolean = true ) : void
		{
			var frameLabelToGo	: String = STATE_INIT_BASE;
			
			// faire un systeme de SWITCH pour savoir quelle etiquette on doit jouer
			
			attainKeyFrame( frameLabelToGo, dispatch );
		}
		
		protected function attainKeyFrame ( value : String, dispatch : Boolean = true ) : void
		{
			if( ! canBrowse || value == lastState || targetState == value )
				return;
			targetState = value;
			
			gotoAndPlay( value );
			if( dispatch )
			{
				dispatchEvent( new Event( value ) );
			}
			canBrowse = false;
		}
		
		override public function setActualSize(newWidth:Number, newHeight:Number):void
		{
		}
		
		override public function getExplicitOrMeasuredWidth() : Number
		{
			return 612; // METTRE ICI LES VALEURS DE boundingBox
		}
		
		override public function getExplicitOrMeasuredHeight() : Number
		{
			return 113.5; // METTRE ICI LES VALEURS DE boundingBox
		}
		
		override public function get width() : Number
		{
			return 612; // METTRE ICI LES VALEURS DE boundingBox
		}
		
		override public function get height() : Number
		{
			return 113.5; // METTRE ICI LES VALEURS DE boundingBox
		}
	}
}